//
//  LLRefreshControl.swift
//  Weibo
//
//  Created by apple on 15/7/4.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class LLRefreshControl: UIRefreshControl {

    override init(){
        super.init()
        //添加自定义refreshview并且设置布局
        addSubview(refreshView)
        refreshView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: CGSize(width: 150, height: 60))
        //用kvo来鉴定LLRefreshControl
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
    
    }
    
    //注销监听
    deinit{
    
    removeObserver(self, forKeyPath: "frame")
    
    }
    /// 正在加载标记
    private var loadingFlag = false
    ///  frame改变触发这个方法
    var showTipFlag = false
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
//        print(frame.origin.y)
        //如果y》0 直接返回  提高效率
        if frame.origin.y > 0{
        return
        }
        if frame.origin.y > -45 && !showTipFlag{
            print("直立")
            refreshView.rotateTipIcon(showTipFlag)
            showTipFlag = true
        }else if frame.origin.y < -45 && showTipFlag{
            print("倒转")
            refreshView.rotateTipIcon(showTipFlag)
            showTipFlag = false
        }
        //开启动画
        
        if refreshing && !loadingFlag{
            refreshView.startLoadingAnim()
            loadingFlag = true
            return
        }
        
    }
    
    override func endRefreshing() {
        
        super.endRefreshing()
        refreshView.stopLoadingAnim()
        loadingFlag = false
    }
    

  required init(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    
    lazy var refreshView = LLRefreshView.refreshView()

}

//llrefreshcontrol类
class LLRefreshView: UIView{
    @IBOutlet weak var TipIcon: UIImageView!
    @IBOutlet weak var LoadingIcon: UIImageView!
    @IBOutlet weak var TipView: UIView!
///  定义一个类函数 返回LLRefreshView
    class func refreshView() ->LLRefreshView {
        
        return NSBundle.mainBundle().loadNibNamed("HMRefreshControl", owner: nil, options: nil).last as! LLRefreshView
    }
    
    func rotateTipIcon(clockWise: Bool) {
        var angle = M_PI
        angle += clockWise ? -0.01 : 0.01
        
        UIView.animateWithDuration(0.5) { () -> Void in
            self.TipIcon.transform = CGAffineTransformRotate(self.TipIcon.transform, CGFloat(angle))
        }
    
    }
    
    ///  开启加载动画
    private func startLoadingAnim() {
        
        TipView.hidden = true
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 0.5
        
        LoadingIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    ///  停止动画
    private func stopLoadingAnim() {
        TipView.hidden = false
        
        LoadingIcon.layer.removeAllAnimations()
    }


}
